package com.gdufe.firesafe.aop;

import com.gdufe.firesafe.exception.RRException;
import com.gdufe.firesafe.model.constants.RoleConstant;
import com.gdufe.firesafe.model.entity.UserEntity;
import com.gdufe.firesafe.utils.TokenUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 自定义切面类
 *
 * @author by
 */
@Aspect
@Component
public class AuthAspect {

    /**
     * 校验管理员权限
     *
     * @param joinPoint 连接点
     */
    @Around("@annotation(com.gdufe.firesafe.annotation.NeedAdmin)")
    public Object judgeAdminAuth(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取当前用户
        UserEntity currentUser = TokenUtils.getCurrentUser();
        //判断角色
        if(currentUser.getRole().equals(RoleConstant.ADMIN)){
            //放行
            return joinPoint.proceed();
        }else{
            throw new RRException(401,"无权限");
        }
    }

    /**
     * 校验业主权限
     *
     * @param joinPoint 连接点
     */
    @Around("@annotation(com.gdufe.firesafe.annotation.NeedOwner)")
    public Object judgeOwnerAuth(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取当前用户
        UserEntity currentUser = TokenUtils.getCurrentUser();
        //判断角色
        if(currentUser.getRole().equals(RoleConstant.OWNER) || currentUser.getRole().equals(RoleConstant.ADMIN)){
            //放行
            return joinPoint.proceed();
        }else{
            throw new RRException(401,"无权限");
        }
    }

    /**
     * 校验安全检查员权限
     *
     * @param joinPoint 连接点
     */
    @Around("@annotation(com.gdufe.firesafe.annotation.NeedInspector)")
    public Object judgeInspectorAuth(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取当前用户
        UserEntity currentUser = TokenUtils.getCurrentUser();
        //判断角色
        if(currentUser.getRole().equals(RoleConstant.INSPECTOR) || currentUser.getRole().equals(RoleConstant.ADMIN)){
            //放行
            return joinPoint.proceed();
        }else{
            throw new RRException(401,"无权限");
        }
    }
}
